package com.sx.zongzhi.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.LocalDataAdapter;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.OrganizingBean;
import com.sx.zongzhi.presenter.OrganizingPest;
import com.sx.zongzhi.utils.Global;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IOrganizingView;
import com.sx.zongzhi.widget.LoadDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 组织机构筛选
 */
public class OrganizingActivity extends BaseActivity implements IOrganizingView{

    @BindView(R.id.tvLocal)
    TextView tvLocal;
    @BindView(R.id.listViewTitle)
    ListView listViewTitle;
    @BindView(R.id.listViewData)
    ListView listViewData;
    private List<OrganizingBean> dataList=new ArrayList<>();
    private List<OrganizingBean> titleList=new ArrayList<>();
    private LocalDataAdapter adapterTitle;
    private OrganizingBean orgbean;
    /**左侧选中的位置*/
    private int titlePosition=0;
    /**当前界面的名字*/
    private String currentPager="";
    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_local);
        ButterKnife.bind(this);
        currentPager=getIntent().getStringExtra(Global.ACTIVITY_FRAGMENT);
        setTitleName("当前位置");
        setTitleRight("确定");
        initData();
    }

    @OnClick({R.id.tv_right})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                MyRxBus.get().post(new MsgEvent<>(currentPager,0,orgbean));
                finish();
                break;
        }
    }

    private void initData() {
        LoadDialog.show(this);
        OrganizingPest presenter=new OrganizingPest(this);
        presenter.getOrganizing();
        listViewTitle.setOnItemClickListener(titleItemClick);
        listViewData.setOnItemClickListener(dataItemClick);
    }



    /**
     * 左侧列表的监听
     */
    AdapterView.OnItemClickListener titleItemClick=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            titlePosition=position;
            adapterTitle.setItemBackground(position);
            showData(titleList.get(position));
            orgbean=titleList.get(position);
        }
    };



    /**
     * 右侧列表的监听
     */
    AdapterView.OnItemClickListener dataItemClick=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            tvLocal.setText("当前位置："+dataList.get(position).getName()+"");
            while (titleList.size()-titlePosition>1){
                titleList.remove(titlePosition+1);
            }
            titleList.add(dataList.get(position));
            adapterTitle.setItemBackground(titleList.size()-1);
            orgbean=dataList.get(position);

            titlePosition=titleList.size()-1;
            adapterTitle.setItemBackground(titlePosition);
            showData(titleList.get(titlePosition));
            orgbean=titleList.get(titlePosition);

        }
    };

    @Override
    public void getOrg(OrganizingBean organizingBean) {
        if (organizingBean!=null){
            tvLocal.setText("当前位置:"+organizingBean.getName());
        }else {
            tvLocal.setText("当前位置：获取失败");
        }
        LoadDialog.dismiss(this);
        orgbean=organizingBean;
        titleList.clear();
        titleList.add(organizingBean);
        adapterTitle=new LocalDataAdapter(this,R.layout.item_text,titleList);
        listViewTitle.setAdapter(adapterTitle);
        adapterTitle.setItemBackground(0);
        showData(organizingBean);
    }

    /**
     * 显示右侧数据
     * @param organizingBean
     */
    private void showData(OrganizingBean organizingBean) {
        dataList.clear();
        try {
            if (organizingBean.getOrgProvinces()!=null) {//省
                dataList.addAll(organizingBean.getOrgProvinces());
            }
            if (organizingBean.getOrgCitys()!=null) {//市
                dataList.addAll(organizingBean.getOrgCitys());
            }
            if (organizingBean.getOrgAreas()!=null) {//区
                dataList.addAll(organizingBean.getOrgAreas());
            }
            if (organizingBean.getOrgStreets()!=null) {//街道
                dataList.addAll(organizingBean.getOrgStreets());
            }
            if (organizingBean.getOrgVillages()!=null){//乡镇
                dataList.addAll(organizingBean.getOrgVillages());
            }
            if (organizingBean.getOrgFrames()!=null){//村
                dataList.addAll(organizingBean.getOrgFrames());
            }
            LocalDataAdapter adapterData=new LocalDataAdapter(this,R.layout.item_text,dataList);
            listViewData.setAdapter(adapterData);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void showError(String message) {
        LoadDialog.dismiss(this);
        ToastUtils.showToast(this,message);
    }
}
